
package com.bh.framework.utils;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;

/**
 * JSON工具类
 * 
 * @author
 * 
 */
public class JSONUtils
{
	/*
	 * 将不包含日期类型的任意对象转换为Json字符串
	 */
	public static String parser2json(Object obj)
	{
		Gson gson = new Gson();
		return gson.toJson(obj);
	}

	/*
	 * 将包含日期类型的任意对象转换为Json字符串
	 */
	public static String parserDateBean2json(Object obj)
	{
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder
				.registerTypeAdapter(
						java.util.Date.class, new DateSerializerUtils())
				.setDateFormat(DateFormat.LONG).create();
		return gson.toJson(obj);
	}

	/*
	 * 返回数组
	 * example:Type listType = new
	 * TypeToken<List<Order>>(){}.getType();将listType传到方法里
	 */
	public static <T> List<T> fromJson(String strjson)
	{
		try
		{
			Gson gs = new Gson();
			Type listType = new TypeToken<List<T>>()
			{
			}.getType();
			List<T> target2 = gs.fromJson(strjson, listType);
			return target2;
		} catch (Exception e)
		{
			return null;
		}
	}

	/*
	 * 返回一个对象
	 */
	public static <T> T fromJson(String strjson, Class<T> cls)
	{
		try
		{
			Gson gs = new Gson();
			// Type listType = new TypeToken<List<T>>(){}.getType();
			T target2 = (T) gs.fromJson(strjson, cls);
			return target2;
		} catch (Exception e)
		{
			return null;
		}
	}

	/*
	 * 返回一个含有Date类型属性的对象
	 */
	public static <T> T fromJson2DateBean(String json, Class<T> cls)
	{
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder
				.registerTypeAdapter(
						java.util.Date.class, new DateDeserializerUtils())
				.setDateFormat(DateFormat.LONG).create();
		return gson.fromJson(json, cls);
	}

	// public static <T> List<T> fromJson2DateBeanList(String strjson) {
	// GsonBuilder gsonBuilder = new GsonBuilder();
	// Gson gson = gsonBuilder.registerTypeAdapter(java.util.Date.class,
	// new DateDeserializerUtils()).setDateFormat(DateFormat.LONG)
	// .create();
	// Type listType = new TypeToken<List<T>>(){}.getType();
	// return gson.fromJson(strjson, listType);
	// }

	/**
	 * 日期序列化实用工具类
	 * 
	 * @author
	 * @date 2011-12-18
	 */
	private static class DateDeserializerUtils implements
			JsonDeserializer<java.util.Date>
	{
		@Override
		public java.util.Date deserialize(JsonElement json, Type type,
				JsonDeserializationContext context) throws JsonParseException
		{
			return new java.util.Date(json.getAsJsonPrimitive().getAsLong());
		}

	}

	/**
	 * 日期解序列实用工具类
	 * 
	 * @author
	 * @date 2011-12-18
	 */
	private static class DateSerializerUtils implements
			JsonSerializer<java.util.Date>
	{
		@Override
		public JsonElement serialize(Date date, Type type,
				JsonSerializationContext content)
		{
			return new JsonPrimitive(date.getTime());
		}
	}

}
